//
//  PublishFeedRequest.m
//  collection
//
//  Created by Jing on 4/6/12.
//  Copyright (c) 2012 Tsinghua University. All rights reserved.
//

#import "PublishFeedRequest.h"

@implementation PublishFeedRequest

- (void)initParams {
    
    self.descriptionName = @"note.publish";
    
    PublishFeedRequestData* requestData = [[PublishFeedRequestData alloc] init];
    self.request = requestData;
    [requestData release];
    
    PublishFeedResponse* response = [[PublishFeedResponse alloc] init];
    self.response = response;
    [response release];
}

- (NSString*)buildRequestURL {
    
    PublishFeedRequestData* requestData = (PublishFeedRequestData*)self.request;
    
    // URLs
    if (requestData.picURLs.count != requestData.picSizes.count) {
        return nil;
    }
    
    NSMutableString* pics = [[NSMutableString alloc] initWithCapacity:256];
    for (int i = 0; i < requestData.picURLs.count; i++) {
        if (i > 0) {
            [pics appendString:@" "];
        }
        
        NSValue* value = [requestData.picSizes objectAtIndex:i];
        CGSize picSize = value.CGSizeValue;
        [pics appendFormat:@"%@_%d_%d", [requestData.picURLs objectAtIndex:i], (int)picSize.width, (int)picSize.height];
    }
    
    // Tags
    NSMutableString* tags = [[NSMutableString alloc] initWithCapacity:256];
    for (int i = 0; i < requestData.tags.count; i++) {
        if (i > 0) {
            [tags appendFormat:@" "];
        }
        
        NSNumber* value = [requestData.tags objectAtIndex:i];
        int picTag = value.intValue;
        [pics appendFormat:@"%d", picTag];
    }
    
    NSString* requestContent = [NSString stringWithFormat:@"&skey=%@&uid=%lld&note=%@&pics=%@&tags=%@",
                                requestData.sessionKey,
                                requestData.UID,
                                requestData.text,
                                pics,
                                tags];

    [pics release];
    [tags release];
    
    return requestContent;
}

- (void)parseResponse:(NSObject *)data {
    
    PublishFeedResponse* response = (PublishFeedResponse*)self.response;
    
    NSString* value = nil;
    NSDictionary* dic = (NSDictionary*)data;
    value = [dic objectForKey:@"noteId"];
    if (value != nil && ![value isEqualToString:@""]) {
        response.feedID = value.longLongValue;
    }
}

@end
